package com.speech.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

import com.ontbee.legacyforks.cn.pedant.SweetAlert.SweetAlertDialog;
import com.speech.MyApplication;
import com.speech.R;
import com.speech.constant.PrefsConstant;
import com.speech.db.SharedPreferenceHelper;
import com.speech.entity.BaseEntity;
import com.speech.http.BaseCallback;
import com.speech.net.BasicCallBack;
//import com.speech.net.HttpCommunicationManager;
import com.speech.net.constant.EnumReqestActionCode;
import com.speech.net.entity.MessageC2S;
import com.speech.net.entity.MessageC2SBuilder;
import com.speech.net.entity.MessageS2C;
import com.speech.result.Result;
import com.speech.utils.DeviceUtil;
import com.speech.utils.JsonProcessor;
import com.speech.utils.TitleBuilder;

import retrofit2.Call;


public class FeedbackActivity extends BaseActivity {

    private EditText edt_feedback;

    private SweetAlertDialog dialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);

        _initView();
    }

    private void _initView() {
        new TitleBuilder(this).setTitleText("意见反馈").setRightTextViewContent("提交").setRightViewListening(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                //TODO: 接口请求 意见反馈
//                MessageC2S msg_c2s = MessageC2SBuilder.buildGeneral_MessageC2S(FeedbackActivity.this, EnumReqestActionCode.意见反馈, "{\"suggestion\":" + "\"" + edt_feedback.getText().toString() + "\"}");
//                String json = JsonProcessor.StcGetJson(msg_c2s);
//                HttpCommunicationManager.getInstance().doPost(json, new BasicCallBack(FeedbackActivity.this, json, new BasicCallBack.BasicCallBackListener() {
//                    @Override
//                    public void success(MessageS2C msg_s2c) {
//                        new SweetAlertDialog(FeedbackActivity.this, SweetAlertDialog.SUCCESS_TYPE)
//                                .setTitleText("感谢提供建议")
//                                .setConfirmText("OK")
//                                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
//                                    @Override
//                                    public void onClick(SweetAlertDialog sweetAlertDialog) {
//                                        sweetAlertDialog.dismiss();
//                                        finish();
//                                    }
//                                })
//                                .show();
//                    }
//
//                    @Override
//                    public void fail(MessageS2C msg_s2c) {
//                        new SweetAlertDialog(FeedbackActivity.this, SweetAlertDialog.WARNING_TYPE)
//                                .setTitleText(msg_s2c.strMsgUI)
//                                .setConfirmText("OK")
//                                .show();
//                    }
//                }));

                String uid,token;
                SharedPreferenceHelper spf = new SharedPreferenceHelper(FeedbackActivity.this, PrefsConstant.FILE_USER_INFO);
                String lUID = (String) spf.getData(PrefsConstant.KEY_USER_INFO_LONG_UID, "");
                uid = lUID;
                String lToken = (String) spf.getData(PrefsConstant.KEY_USER_INFO_LONG_TOKEN, "");
                token = lToken;
                Call<Result<BaseEntity>> call = MyApplication.getInstance().getApiService()
                        .suggestion(uid, DeviceUtil.getDeviceID(FeedbackActivity.this)
                                ,token,edt_feedback.getText().toString());
                call.enqueue(new BaseCallback<BaseEntity>() {
                    @Override
                    public void onSuccess(BaseEntity baseEntity) {
//                        dialog = new SweetAlertDialog(FeedbackActivity.this, SweetAlertDialog.SUCCESS_TYPE)
//                                .setTitleText("感谢提供建议")
//                                .setConfirmText("OK")
//                                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
//                                    @Override
//                                    public void onClick(SweetAlertDialog sweetAlertDialog) {
//                                        sweetAlertDialog.dismiss();
//                                        finish();
//                                    }
//                                });
//                                dialog.show();
                    }

                    @Override
                    public void onAbnormal(String msg) {
//                        dialog = new SweetAlertDialog(FeedbackActivity.this, SweetAlertDialog.WARNING_TYPE)
//                                .setTitleText(msg)
//                                .setConfirmText("OK");
//                                dialog.show();
                    }

                    @Override
                    public void onFailure(String msg) {
//                        dialog = new SweetAlertDialog(FeedbackActivity.this, SweetAlertDialog.WARNING_TYPE)
//                                .setTitleText(msg)
//                                .setConfirmText("OK");
//                                dialog.show();
                    }
                });
            }
        });

        edt_feedback = findViewById(R.id.edt_feedback);


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialog != null){
            dialog.dismissWithAnimation();
        }
    }
}
